package com.parktech.sun.web.controller;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.notice.Notice;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.notice.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * User: Think
 * Date: 11-1-16
 * Time: 下午6:00
 */
@Controller
@RequestMapping("/notices")
public class NoticeViewController extends BaseViewController {

    private int pageSize = Constants.PAGINATION_PAGE_SIZE;

    private NoticeService noticeService;

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Autowired
    public NoticeViewController(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

    @RequestMapping("/pages/{pageNo}")
    public String view(@PathVariable("pageNo") int pageNo, Model model) {
        Page page = new Page(pageSize, pageNo);
        model.addAttribute(noticeService.pageBy(page));
        return "notices";
    }

    @RequestMapping("/{noticeId}")
    public String viewDetail(@PathVariable("noticeId") Long noticeId, Model model) {
        Notice notice = noticeService.getById(noticeId);
        model.addAttribute(notice);
        return "noticeDetail";
    }
}
